<?php

namespace frontend\models;

use Yii;
use backend\models\course\LearnLog;
use backend\models\course\CoursePeriod;

class UserCourse extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_course}}';
    }

    /**
     * 加入课程
     */
    public static function AddCourse($user_id,$course_id)
    {
    	$UserCourse = new UserCourse;
		$UserCourse->user_id   = $user_id;
		$UserCourse->course_id = $course_id;
		$UserCourse->add_time  = time();
		$re = $UserCourse->save();
		return $re;
    }

    public static function MyCourse($user_id,$course_id){
       $LearnedNum = LearnLog::find()->where("user_id=$user_id and course_id=$course_id and status='finished'")->count();
       $PeriodNum = CoursePeriod::find()->where("course_id=$course_id and movie_name is not null")->count();
       if ($LearnedNum==$PeriodNum) {
            $isLearned =1;
       }else{
            $isLearned =0;
        }
            $UserCourse = UserCourse::find()->where("user_id=$user_id and course_id=$course_id")->one();
            $UserCourse->learnedNum =$LearnedNum;
            $UserCourse->isLearned =$isLearned;
            $UserCourse->save();
    }
}
